package com.loong.shop.handler;

import com.loong.common.domain.BaseQuery;
import com.loong.common.domain.Pager;
import com.loong.common.exception.GlobalException;
import com.loong.common.executor.BatchExecutor;
import com.loong.common.mybatis.tools.PageTools;
import com.loong.common.redis.RedisTools;
import com.loong.common.utils.TransformUtils;
import com.loong.global.constant.RedisKey;
import com.loong.shop.business.GoodsBaseBusiness;
import com.loong.shop.common.backend.param.GoodsBaseParam;
import com.loong.shop.common.backend.param.GoodsBaseQueryParam;
import com.loong.shop.common.backend.result.GoodsBaseResult;
import com.loong.shop.common.backend.result.GoodsSkuResult;
import com.loong.shop.common.forehead.param.GoodsBaseQueryForeheadParam;
import com.loong.shop.common.forehead.result.GoodsBaseDtoForeheadResult;
import com.loong.shop.mapper.GoodsBaseMapper;
import com.loong.shop.model.GoodsBase;
import com.loong.shop.model.dto.GoodsBaseDto;
import com.loong.shop.param.GoodsBaseDtoQueryParam;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description: 处理商品业务的handler
 * @Author: 刘群
 * @Date: 2020/10/27 8:56
 */
@Component
public class GoodsBaseHandler {

    @Resource
    private GoodsBaseBusiness baseBusiness;

    @Resource
    private GoodsBaseMapper baseMapper;

    @Resource
    private GoodsSkuHandler skuHandler;

    @Resource
    private BatchExecutor batchExecutor;

    /**
     * 后台新增商品
     * @param param 商品封装的参数
     */
    public void saveGoodsBase(GoodsBaseParam param){
        param.setCreateTime(new Date());
        baseBusiness.save(TransformUtils.transform(param, GoodsBase.class));
    }

    /**
     * 后台更新商品
     * @param param 商品封装的参数
     */
    public void updateGoodsBase(GoodsBaseParam param){
        param.setLastUpdateTime(new Date());
        baseBusiness.updateByPrimaryKeySelective(TransformUtils.transform(param, GoodsBase.class));
    }



    /**
     * 后台批量删除商品
     * @param idList 商品id集合
     */
    public void deleteGoodsBase(List<Long> idList){
        batchExecutor.batchExecute(idList,id->id.toString(),(item)->{
            //查询该商品是否被商品详情使用，若有则不能删除，若没有则可以直接删除
            List<GoodsSkuResult> goodsSkuList = skuHandler.findGoodsSkuById(item);
            if (CollectionUtils.isEmpty(goodsSkuList)){
                baseBusiness.deleteById(item);
            }else {
                throw new GlobalException("GoodsBase is used");
            }
        });
    }

    /**
     * 后台分页查询商品
     * @param query 查询参数
     * @return 分页结果
     */
    public Pager<GoodsBaseResult> pageQueryGoodsBase(BaseQuery query,GoodsBaseQueryParam param){
        Pager<GoodsBase> pager = baseBusiness.pageQuery(query, () -> baseBusiness.findGoodsBaseByParam(param));
        return TransformUtils.transformPage(pager, GoodsBaseResult.class);
    }

    /**
     * 后台根据查询参数查询商品
     * @param param 查询参数
     * @return 商品集合
     */
    protected List<GoodsBaseResult> findGoodsBaseByParam(GoodsBaseQueryParam param){
        return TransformUtils.transformList(baseBusiness.findGoodsBaseByParam(param),GoodsBaseResult.class);
    }

    /**
     * 前台分页进行联合查询商品列表
     * @param param 查询参数
     * @return 分页结果
     */
    public Pager<GoodsBaseDtoForeheadResult> pageQueryGoodsBaseListToForehead(GoodsBaseQueryForeheadParam param){
        GoodsBaseDtoQueryParam queryParam = TransformUtils.transform(param, GoodsBaseDtoQueryParam.class);
        Pager<GoodsBaseDto> pager = PageTools.pageQuery(param, () -> baseMapper.joinQueryGoodsBaseList(queryParam));
        return TransformUtils.transformPage(pager,GoodsBaseDtoForeheadResult.class);
    }

    /**
     * 前台点赞商品时将Redis中存放的该点赞数+1
     * @param goodsBaseId 商品id
     */
    public void likeGoodsBase(Long goodsBaseId){
        //点赞数+1：
        RedisTools.valueOperations().increment(RedisKey.GOODS_BASE.GOODS_ID+goodsBaseId+RedisKey.GOODS_BASE.LIKE_NUM);
    }

    /**
     * 前台更新商品点赞数到数据库
     * @param goodsBaseId 商品id
     */
    public void updateGoodsBaseLikNum(Long goodsBaseId){
        //从Redis中获取商品的点赞数
        Integer likeNum = (Integer) RedisTools.valueOperations().get(RedisKey.GOODS_BASE.GOODS_ID + goodsBaseId + RedisKey.GOODS_BASE.LIKE_NUM);
        if (likeNum == null) likeNum = 0;
        GoodsBaseParam param = GoodsBaseParam.builder()
                .id(goodsBaseId)
                .likeNum(likeNum.longValue())
                .lastUpdateTime(new Date())
                .build();
        //更新商品点赞数到数据库
       baseBusiness.updateByPrimaryKeySelective(TransformUtils.transform(param,GoodsBase.class));
    }

}

